<?php
namespace Home\Controller;
use Common\Controller\CommonController;
use Common\Model\UserRelationModel;
use Common\Api\SendPhoneCodeApi;
use Common\Model\MemberModel;
class HomeController extends CommonController {
	public function _initialize(){
		/* $kg = $this->getConfig()['wangzhan_kg'];
		if($kg!=1){
			echo "<h1>系统升级中....</h1>";die;
		} */
 		parent::_initialize();
		//header('Access-Control-Allow-Origin: *');
		if($_SERVER['REQUEST_METHOD'] == 'OPTIONS'){
		header("Access-Control-Allow-Origin: *");
		header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
		header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE,OPTIONS,PATCH');

}
	}
	public function checkTime($type,$uid){
		$time = time();
		$addtime = session('MONEY_TIME'.$type.$uid);
		if($addtime!= ""&&$time-$addtime<5){
			return false;
		}else{
			session('MONEY_TIME'.$type,time());
			return true;
		}
	}
	public function getPrice(){
	    $list = M('Price')->find();
	    return $list;
	}
	//发送验证码
	public function sendPhone(){
		$phone = I('phone')?I('phone'):null;
		$type = I('type')?I('type'):1;
		$code_name = 'zhuce';
		if(empty($phone)){
			$data_return['status'] = -41;
			$data_return['info']   = '请输入手机号';
			return $this->ajaxReturn($data_return);
		}
		if(!regex($phone,'phone')){
			$data_return['status'] = -8;
			$data_return['info']   = '手机号格式错误';
			return $this->ajaxReturn($data_return);
		}
		switch($type){
			case 1:$code_name = 'zhuce';
			case 2:$code_name = 'zhaohuimima';
			case 3:$code_name = 'shiming';
			case 4:$code_name = 'jiaoyi';
		}
		$USER = new MemberModel();
		$checkphone = $USER->getUserByPhone($phone);
		//注册的时候检测手机号是否注册
		if($type==1){
			if($checkphone){
				$data_return['status'] = -42;
				$data_return['info']   = '手机号已存在';
				return $this->ajaxReturn($data_return);
			}
		}
		//找回密码
		if($type==2){
			if(!$checkphone){
				$data_return['status'] = -42;
				$data_return['info']   = '手机号不存在';
				return $this->ajaxReturn($data_return);
			}
		}
		$SENDPHONE = new SendPhoneCodeApi($phone);
		$send = $SENDPHONE->send($code_name);
		if($send == 1){
			$data_return['status'] = 1;
			$data_return['info']   = '操作成功';
			return $this->ajaxReturn($data_return);
		}else{
			$data_return['status'] = -1;
			$data_return['info']   = $send;
			return $this->ajaxReturn($data_return);
		}
	}
	/**
	 *
	 获取用户资金
	 */
	public function  getUserMoney($member_id,$type){
	    $where['member_id']=$member_id;
	    $usermoney = M('Member_money')->where($where)->find()['money'.$type];
	    return $usermoney;
	}
	//图片处理
	public function upload($file){
	    switch($file['type'])
	    {
	        case 'image/jpeg': $ext = 'jpg'; break;
	        case 'image/gif': $ext = 'gif'; break;
	        case 'image/png': $ext = 'png'; break;
	        case 'image/tiff': $ext = 'tif'; break;
	        default: $ext = ''; break;
	    }
	    if (empty($ext)){
	        return false;
	    }
	    $upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->savePath  =      './Uploads/'; // 设置附件上传目录
	    // 上传文件
	    $info   =  $upload->uploadOne($file);
	    if(!$info) {
	        // 上传错误提示错误信息
	        $this->error($upload->getError());exit();
	    }else{
	        // 上传成功
	        $pic=$info['savepath'].$info['savename'];
	        $url='/Uploads'.ltrim($pic,".");
	    }
	    return $url;
	}
}